home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / system files / TrapWeaver 1.05 / tweaver.exe / sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-02  |  3.8 KB  |  141 lines

  1. /*
  2.  
  3. Sample Patching Code for TrapWeaver
  4.  
  5. Version 1 - Written by Josh Freeman
  6.  
  7. (c)1998 Twilight Edge Software
  8.  
  9. Email:  twilight_edge@hotmail.com
  10.  
  11. This file may be freely distributed provided it is unchanged.
  12.  
  13. **********************************************************************
  14.  
  15. This file contains four sample functions which show how to check if TrapWeaving is engaged,
  16. acquire the TrapWeaver suite, and set up & destroy a system patch.    
  17.  
  18. The TrapWeaver suite is NOT necessary in order to make use of TrapWeaver's patch management 
  19. functionality. All programs that call SysSetTrapAddress() will be protected from conflicting
  20. with each other when TrapWeaving is turned on - resetting a trap to its previous address will
  21. not affect patches that were placed after yours.
  22.  
  23. Error checking has been reduced in order to make the code easier to read.
  24.  
  25. */
  26.  
  27. #include <Pilot.h>
  28.  
  29. #include "tw_suite.h"
  30.  
  31. /**********************************************************************/
  32.  
  33. #define kCreatorID                        '????'    // Your app's creator ID here
  34.  
  35. //    kpatchTimGetSecondsCodeSize is the size of the patch (rounded up to nearest 10), 
  36. //    obtained by disassembling the source file. (In Codewarrior, select 'Disassemble' from
  37. //    the Project menu with a source file as the active window)
  38. #define kpatchTimGetSecondsCodeSize        70    
  39.  
  40. /**********************************************************************/
  41.  
  42. // This function checks to see if TrapWeaver is installed, and returns a pointer to the suite
  43. //    Pass NULL for 'suiteHandle' if you don't need the suite returned
  44. Boolean IsTrapWeavingOn(TrapWeaverSuite1 **suiteHandle);
  45.  
  46. // This function installs the patch
  47. Err     SetupMyPatch(void);    
  48.  
  49. // This function removes the patch                                    
  50. Err     RemoveMyPatch(void);
  51.  
  52. // This is the patch for TimGetSeconds()
  53. ULong     patchTimGetSeconds(void);                            
  54.  
  55. /**********************************************************************/
  56.  
  57. Boolean IsTrapWeavingOn(TrapWeaverSuite1 **suiteHandle)
  58. {
  59.     Err error;
  60.     TrapWeaverSuite1 *twSuite;
  61.     
  62.     error = FtrGet(kTrapWeaverCreatorID, kTrapWeaverSuiteFtr, (DWordPtr) (&twSuite));
  63.     
  64.     if (error != 0)
  65.         return false;
  66.     
  67.     if (suiteHandle != NULL)
  68.         *suiteHandle = twSuite;    
  69.     
  70.     return true;
  71. }
  72.  
  73. /**********************************************************************/
  74.  
  75. Err SetupMyPatch(void)
  76. {
  77.     Err error = 0;
  78.     TrapWeaverSuite1 *twSuite;
  79.     VoidPtr    oldTrap;
  80.     
  81.     // Return if TrapWeaving is not engaged
  82.     if (!IsTrapWeavingOn(&twSuite))
  83.         return -1;
  84.  
  85.     // Set up patch
  86.     oldTrap = SysGetTrapAddress(sysTrapTimGetSeconds);
  87.     
  88.     FtrSet(kCreatorID, sysTrapTimGetSeconds, (DWord) oldTrap);
  89.  
  90. /*
  91. The suite function, SetupPatch() makes patching easier by copying your code to a safe, 
  92. locked location & changing the trap address all in one function call. Your patches no longer
  93. have to be placed in a separate resource or source file, manually locked, then patched.
  94. The tradeoff is that you will have to keep track of the size of the patch, which can be
  95. found by disassembling the source file.
  96. */
  97.     
  98.     error = twSuite->SetupPatch(sysTrapTimGetSeconds, patchTimGetSeconds,
  99.                                 kpatchTimGetSecondsCodeSize);
  100.                                 
  101.     return error;
  102. }
  103.  
  104. /**********************************************************************/
  105.  
  106. Err RemoveMyPatch(void)
  107. {
  108.     Err error;
  109.     VoidPtr oldTrap;
  110.     
  111.     // Get old trap address
  112.     error = FtrGet(kCreatorID, sysTrapTimGetSeconds, (DWordPtr) &oldTrap);
  113.     
  114.     // Restore the trap to the old address
  115.     if (error == 0)
  116.     {
  117.         SysSetTrapAddress(sysTrapTimGetSeconds, oldTrap);
  118.         FtrUnregister(kCreatorID, sysTrapTimGetSeconds);
  119.     }
  120.     
  121.     return error;
  122. }
  123.  
  124. /**********************************************************************/
  125.  
  126. ULong patchTimGetSeconds(void)
  127. {
  128.     ULong (*oldTrap) (void);
  129.     ULong returnValue;
  130.     
  131.     // Do stuff here...
  132.     
  133.     // Call old trap
  134.     FtrGet(kCreatorID, sysTrapTimGetSeconds, (DWordPtr) &oldTrap);
  135.     returnValue = oldTrap();
  136.     
  137.     // ...or do stuff here
  138.  
  139.     return returnValue;
  140. }
  141.